<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/3/3
 * Time: 10:18
 */

namespace app\admin\controller;


use think\Db;

class Beautydiaryreport extends Base
{
    /**
     * 美丽日记举报列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $user_keyword = input('user_keyword'); // 举报人昵称/手机号
            $other_user_keyword = input('other_user_keyword'); // 举报人昵称/手机号
            $bdr_bdrt_id = input('bdr_bdrt_id'); // 举报分类
            $bdr_status = input('bdr_status');
            
            
            $where = [];
            if(isset($user_keyword) && $user_keyword != ''){
                $where['u1.user_username|u1.user_phone'] = ['like', '%' . $user_keyword . '%'];
            }
            if(isset($other_user_keyword) && $other_user_keyword != ''){
                $where['u2.user_username|u2.user_phone'] = ['like', "%$other_user_keyword%"];
            }
            if(isset($bdr_bdrt_id) && $bdr_bdrt_id != ''){
                $where['bdr_bdrt_id'] = $bdr_bdrt_id;
            }
            if(isset($bdr_status) && $bdr_status != ''){
                $where['bdr_status'] = $bdr_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('beauty_diary_report')
                ->join('up_u_user u1', 'bdr_user_id = u1.user_id', 'LEFT')
                ->join('up_u_user u2', 'bdr_other_user_id = u2.user_id', 'LEFT')
                ->join('up_beauty_diary_report_type', 'bdrt_id = bdr_bdrt_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('beauty_diary_report')
                ->join('up_u_user u1', 'bdr_user_id = u1.user_id', 'LEFT')
                ->join('up_u_user u2', 'bdr_other_user_id = u2.user_id', 'LEFT')
                ->join('up_beauty_diary_report_type', 'bdrt_id = bdr_bdrt_id', 'LEFT')
                ->field('bdr_id, u1.user_username, u1.user_phone, u2.user_username other_user_username, u2.user_phone other_user_phone, 
                bdr_bd_id, bdr_status, bdr_time, bdrt_name, bdr_type, bdr_comment_id')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('bdr_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $report_type = Db::table('up_beauty_diary_report_type')
            ->where('bdrt_status', 1)
            ->field('bdrt_id, bdrt_name')
            ->select();
        $this->assign('report_type', $report_type);
        return $this->fetch();
    }
    
    /**
     * 举报详情
     * @return mixed
     */
    public function reportDetail(){
        $bdr_id = input('bdr_id');
        $report = Db::table('up_beauty_diary_report')
            ->join('up_u_user u1', 'bdr_user_id = u1.user_id', 'LEFT')
            ->join('up_u_user u2', 'bdr_other_user_id = u2.user_id', 'LEFT')
            ->join('up_beauty_diary_report_type', 'bdrt_id = bdr_bdrt_id', 'LEFT')
            ->where('bdr_id', $bdr_id)
            ->field('u1.user_username, u1.user_phone, u2.user_username other_user_username, 
            u2.user_phone other_user_other, bdr_bd_id, bdrt_name, bdr_status, bdr_content, 
            bdr_time, bdr_user_id, bdr_other_user_id, bdr_comment_id, bdr_type')
            ->find();
        return $this->fetch('beautydiaryreport/reportdetail', ['report' => $report]);
    }
    
    /**
     * 处理
     * @return \think\response\Json
     */
    public function reportDone(){
        $bdr_id = input('id');
        $res = Db::table('up_beauty_diary_report')->where('bdr_id', $bdr_id)->setField('bdr_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '处理失败']);
        }
    }
    
    /**
     * 删除举报
     * @return \think\response\Json
     */
    public function reportDel(){
        $bdr_id = input('id');
        $res = Db::table('up_beauty_diary_report')->delete($bdr_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}